<?php
class ModelMarketingCoupon extends Model {
    public function addCoupon($data) {
        $this->event->trigger('pre.admin.coupon.add', $data);

        $this->db->query("INSERT INTO " . DB_PREFIX . "coupon SET name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', discount = '" . (float)$data['discount'] . "', type = '" . $this->db->escape($data['type']) . "', total = '" . (float)$data['total'] . "', logged = '" . (int)$data['logged'] . "', shipping = '" . (int)$data['shipping'] . "', date_start = '" . $this->db->escape($data['date_start']) . "', date_end = '" . $this->db->escape($data['date_end']) . "', uses_total = '" . (int)$data['uses_total'] . "', uses_customer = '" . (int)$data['uses_customer'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");

        $coupon_id = $this->db->getLastId();

        if (isset($data['coupon_product'])) {
            foreach ($data['coupon_product'] as $product_id) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "coupon_product SET coupon_id = '" . (int)$coupon_id . "', product_id = '" . (int)$product_id . "'");
            }
        }

        if (isset($data['coupon_category'])) {
            foreach ($data['coupon_category'] as $category_id) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "coupon_category SET coupon_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'");
            }
        }

        $this->event->trigger('post.admin.coupon.add', $coupon_id);

        return $coupon_id;
    }

    //添加券的消费记录
    public function addUseCouponHistory($data){
        $this->db->query("INSERT INTO " . DB_PREFIX . "coupon_history SET coupon_id = '" . (int)$data['coupon_id'] . "', order_id = '" . (int)$data['order_id'] . "', customer_id = '" . (int)$data['customer_id'] . "', amount = '" . (float)$data['amount'] . "', date_added = NOW()");

    }

    public function editCoupon($coupon_id, $data) {
        $this->event->trigger('pre.admin.coupon.edit', $data);

        $this->db->query("UPDATE " . DB_PREFIX . "coupon SET name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', discount = '" . (float)$data['discount'] . "', type = '" . $this->db->escape($data['type']) . "', total = '" . (float)$data['total'] . "', logged = '" . (int)$data['logged'] . "', shipping = '" . (int)$data['shipping'] . "', date_start = '" . $this->db->escape($data['date_start']) . "', date_end = '" . $this->db->escape($data['date_end']) . "', uses_total = '" . (int)$data['uses_total'] . "', uses_customer = '" . (int)$data['uses_customer'] . "', status = '" . (int)$data['status'] . "' WHERE coupon_id = '" . (int)$coupon_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "coupon_product WHERE coupon_id = '" . (int)$coupon_id . "'");

        if (isset($data['coupon_product'])) {
            foreach ($data['coupon_product'] as $product_id) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "coupon_product SET coupon_id = '" . (int)$coupon_id . "', product_id = '" . (int)$product_id . "'");
            }
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");

        if (isset($data['coupon_category'])) {
            foreach ($data['coupon_category'] as $category_id) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "coupon_category SET coupon_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'");
            }
        }

        $this->event->trigger('post.admin.coupon.edit', $coupon_id);
    }

    public function deleteCoupon($coupon_id) {
        $this->event->trigger('pre.admin.coupon.delete', $coupon_id);

        $this->db->query("DELETE FROM " . DB_PREFIX . "coupon WHERE coupon_id = '" . (int)$coupon_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "coupon_product WHERE coupon_id = '" . (int)$coupon_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "coupon_history WHERE coupon_id = '" . (int)$coupon_id . "'");

        $this->event->trigger('post.admin.coupon.delete', $coupon_id);
    }

    //优惠券是否过期
    public function coupon_Isoverdue($coupon_id){
        $query = $this->db->query("SELECT count(*) as total FROM " . DB_PREFIX . "coupon WHERE coupon_id = '" . (int)$coupon_id . "' date_start <= now() and date_end >= now() ");
        if($query->row['total']>0){
            return  true;
        }else{
            return  false;
        }
    }

    //用户获取优惠券
    public function addCustomerCoupon($coupon_id){
        $this->db->query("INSERT INTO " . DB_PREFIX . "customer_coupon SET customer_id = '" . $this->customer->getId() . "', coupon_id = '" . $this->db->escape($coupon_id) . "', date_added = now() ");
        $id = $this->db->getLastId();
        $coupon_num = $this->db->query("SELECT uses_total  FROM " . DB_PREFIX . "coupon WHERE coupon_id = '" . (int)$coupon_id . "' ")->row['uses_total'];

        $this->db->query(" UPDATE " . DB_PREFIX . "coupon SET uses_total = '" . ((int)$coupon_num - 1). "' WHERE coupon_id = '" . (int)$coupon_id . "'");
        return $id;
    }




    public function getCoupon($coupon_id) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "coupon WHERE coupon_id = '" . (int)$coupon_id . "' and uses_total > 0 ");

        return $query->row;
    }

    public function getCouponByCode($code) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "coupon WHERE code = '" . $this->db->escape($code) . "'");

        return $query->row;
    }

    public function getCoupons() {
        $query = $this->db->query("SELECT  * FROM " . DB_PREFIX . " coupon where date_start <= now() and date_end >= now() and status = 1 and uses_total >0 and coupon_id != 6 ");

        return $query->rows;
    }

    public function getCouponProducts($coupon_id) {
        $coupon_product_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_product WHERE coupon_id = '" . (int)$coupon_id . "'");

        foreach ($query->rows as $result) {
            $coupon_product_data[] = $result['product_id'];
        }

        return $coupon_product_data;
    }

    public function getCouponCategories($coupon_id) {
        $coupon_category_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");

        foreach ($query->rows as $result) {
            $coupon_category_data[] = $result['category_id'];
        }

        return $coupon_category_data;
    }

    public function getTotalCoupons() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "coupon");

        return $query->row['total'];
    }

    public function getCouponHistories($coupon_id, $start = 0, $limit = 10) {
        if ($start < 0) {
            $start = 0;
        }

        if ($limit < 1) {
            $limit = 10;
        }

        $query = $this->db->query("SELECT ch.order_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, ch.amount, ch.date_added FROM " . DB_PREFIX . "coupon_history ch LEFT JOIN " . DB_PREFIX . "customer c ON (ch.customer_id = c.customer_id) WHERE ch.coupon_id = '" . (int)$coupon_id . "' ORDER BY ch.date_added ASC LIMIT " . (int)$start . "," . (int)$limit);

        return $query->rows;
    }

    public function getTotalCouponHistories($coupon_id) {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "coupon_history WHERE coupon_id = '" . (int)$coupon_id . "'");

        return $query->row['total'];
    }

    public function getCustomerCoupons(){
        $query = $this->db->query("SELECT cp.*,ccp.customer_id,ccp.is_use,ccp.num FROM " . DB_PREFIX . "customer_coupon as ccp left join ". DB_PREFIX." coupon as cp on ccp.coupon_id = cp.coupon_id WHERE customer_id = '" . (int)$this->customer->getId() . "' and ccp.is_use = 0 ");
        return $query->rows;
    }

    public function isgetCoupon($coupon_id){
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_coupon WHERE coupon_id = '" . (int)$coupon_id . "' and customer_id = '" . (int)$this->customer->getId() . "'");
        if($query->row['total']>0){
            return  true;
        }else{
            return  false;
        }
    }
}